import { getInfo, getUserId } from '@/api/user'
import { resetRouter } from '@/router'
import { getToken, removeToken, setToken } from '@/utils/auth'
import { Message } from 'element-ui'
import { login } from '../../api/user'

export default {
  // 开启命名空间
  namespaced: true,
  // 核心配置项
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    upToken(state, token) {
      state.token = token
      setToken(token)
    },
    delToken(state) {
      state.token = ''
      removeToken()
    },
    getUp(state, info) {
      state.userInfo = info
    },
    delInfo(state) {
      state.userInfo = {}
    }
  },
  actions: {
    async getUpInfo(context) {
      const res = await getInfo()
      // console.log(res, 'resssss')
      const info = await getUserId(res.data.userId)
      // console.log(info)
      // console.log(res)
      context.commit('getUp', { ...res.data, ...info.data })
      return res.data.roles.menus
    },
    delUpInfo(context) {
      context.commit('delToken')
      context.commit('')
      // 重置路由
      resetRouter()
    },
    async initToken(context, hFrom) {
      const res = await login(hFrom)
      // console.log(res)
      if (res.code !== 10000) return Message.error(res.message)
      Message.success(res.message)
      context.commit('upToken', res.data)
    }
  }
}

